import { Button, Input, Node } from "cc";
import { eShiohoEvent, shiohoEventManager } from "../event/shiohoEventManager";
import { baseGuideMediator } from "./baseGuideMediator";
import { guideUtil } from "./guideUtil";
import { GuideForceType, IGuideForceArgs, IGuideForceClickArgs, IGuideForceMoveArgs } from "./iGuide";

interface IForceGuide {
    stepId: number;
    args: IGuideForceArgs;
    nodes: Node[];
}

export class forceGuideMediator extends baseGuideMediator {

    private _curForceGuide: IForceGuide;

    protected onRun(args?: any) {
        this._curForceGuide = <IForceGuide>{
            stepId: this._stepId,
            args: args,
            nodes: []
        }
        switch (args.type) {
            case GuideForceType.Click:
                this.executeClickForce();
                break;
            case GuideForceType.Move:
                this.executeMoveForce();
                break;
            default:
                console.error(`未定义当前类型的解析，请检查：${args.type}`);
                return;
        }
    }

    protected onUpdate(dt: number) {
        if (!this._curForceGuide)
            return;

        //每帧check吧
        if (this._curForceGuide.args.type == GuideForceType.Move) {
            var moveArgs = this._curForceGuide.args as IGuideForceMoveArgs;

            if (this._curForceGuide.nodes.length < 2)
                return;

            if (moveArgs.stepCondition && !moveArgs.stepCondition(this._curForceGuide.nodes))
                return;

            //松手时去刷新遮罩显示      
            this._curForceGuide.nodes.forEach(node => {
                var btn = node.getComponent(Button);
                if (btn) {
                    node.off(Button.EventType.CLICK, this.refreshMoveGuideShow, this);
                } else {
                    node.off(Input.EventType.TOUCH_END, this.refreshMoveGuideShow, this);
                }
            })
            this.stepComplete();
            this._curForceGuide = null;
        }
    }

    protected onComplete() {
        //关闭引导页面
        shiohoEventManager.getInstance().dispatch(eShiohoEvent.SwitchForcePanel, false);
    }

    private executeClickForce() {
        var args = this._curForceGuide.args as IGuideForceClickArgs;
        var locators = args.locators();
        if (this.setNodes(this._curForceGuide, locators) <= 0) {
            console.error(`未找到当前定义的Node点击节点，请检查：${locators}`)
            return;
        }
        //点击完成
        var clickNode = this._curForceGuide.nodes[0];
        //绑定点击step监听
        var btn = clickNode.getComponent(Button);
        if (btn) {
            clickNode.on(Button.EventType.CLICK, this.onClickForce, this);
        } else {
            clickNode.on(Input.EventType.TOUCH_END, this.onClickForce, this);
        }

        //刷新页面点击显示
        shiohoEventManager.getInstance().dispatch(eShiohoEvent.SwitchForcePanel, true);
        var params = { type: GuideForceType.Click, node: clickNode, tips: args.tips };
        shiohoEventManager.getInstance().dispatch(eShiohoEvent.RefreshForcePanel, params);
    }

    private executeMoveForce() {
        var args = this._curForceGuide.args as IGuideForceMoveArgs;
        if (this.setNodes(this._curForceGuide, args.locators) < 2) {
            console.error(`未找到2个当前定义的Node移动节点，请检查：${args.locators}`);
            return;
        }
        //松手时去刷新遮罩显示      
        this._curForceGuide.nodes.forEach(node => {
            var btn = node.getComponent(Button);
            if (btn) {
                node.on(Button.EventType.CLICK, this.refreshMoveGuideShow, this);
            } else {
                node.on(Input.EventType.TOUCH_END, this.refreshMoveGuideShow, this);
            }
        })
        this.refreshMoveGuideShow();
    }

    private setNodes(guide: IForceGuide, locators: string[]): number {
        guide.nodes = [];
        locators.forEach(locator => {
            var nodes = guideUtil.findNodes(locator);
            nodes.forEach(node => {
                if (node.active) {
                    guide.nodes.push(node);
                }
            })
        });
        return guide.nodes.length;
    }

    private stepComplete() {
        this.stepOver(this._guideId, this._curForceGuide.stepId);
        //关闭页面
        shiohoEventManager.getInstance().dispatch(eShiohoEvent.SwitchForcePanel, false);
    }

    private refreshMoveGuideShow() {
        var args = this._curForceGuide.args as IGuideForceMoveArgs;
        //刷新页面滑动显示
        shiohoEventManager.getInstance().dispatch(eShiohoEvent.SwitchForcePanel, true);
        var params = { type: GuideForceType.Move, nodes: this._curForceGuide.nodes, moveTime: args.moveTime };
        shiohoEventManager.getInstance().dispatch(eShiohoEvent.RefreshForcePanel, params);
    }

    private onClickForce() {
        var args = this._curForceGuide.args as IGuideForceClickArgs;

        //刷新页面提示
        shiohoEventManager.getInstance().dispatch(eShiohoEvent.RefreshForceTips, args.tips);

        if (args.stepCondition && !args.stepCondition())
            return;

        var clickNode = this._curForceGuide.nodes[0];
        var btn = clickNode.getComponent(Button);
        if (btn) {
            clickNode.off(Button.EventType.CLICK, this.onClickForce, this);
        } else {
            clickNode.off(Input.EventType.TOUCH_END, this.onClickForce, this);
        }
        this.stepComplete();
    }

}


